package com.asset.inventory.view

import android.app.Activity
import android.content.Intent
import android.database.CursorJoiner
import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.lifecycle.Observer
import com.asset.inventory.BR
import com.asset.inventory.R
import com.asset.inventory.base.BaseActivity
import com.asset.inventory.base.BaseApp
import com.asset.inventory.constant.PageType
import com.asset.inventory.databinding.ActivityModifyAssetsBinding
import com.asset.inventory.kt.toast
import com.asset.inventory.manager.ConfigInfoManager
import com.asset.inventory.model.ConfigInfo
import com.asset.inventory.model.TaskDetailBean
import com.asset.inventory.viewModel.ModifyAssetsViewModel

/**
 * 页面名称：资产修改页面
 * 创建时间  2021/04/10 15:47
 */
class ModifyAssetsActivity: BaseActivity<ModifyAssetsViewModel, ActivityModifyAssetsBinding>() {

    private  var departmentInfo: ConfigInfo? = null
    private  var userInfo: ConfigInfo? = null
    private  var locationInfo: ConfigInfo? = null

    override fun getLayoutId(): Int = R.layout.activity_modify_assets

    override fun initVariableId(): Int = BR.modifyAssetsViewModel

    override fun initViewModel(): Class<ModifyAssetsViewModel> = ModifyAssetsViewModel::class.java

    private lateinit var back: TextView


    override fun initView() {
        val taskId = intent.extras?.getLong("TASK_ID")
        val taskDetail = intent.extras?.getSerializable("TASK_DETAIL") as TaskDetailBean.Detail
        mViewModel.bindingData(taskDetail)

        val title = binding.titlebar.findViewById<TextView>(R.id.title)
        back = binding.titlebar.findViewById(R.id.left)
        val exit = binding.titlebar.findViewById<TextView>(R.id.right)
        exit.visibility = View.INVISIBLE
        back.visibility = View.VISIBLE
        back.text = "返回"
        title.text = "资产配置修改"
        mViewModel.setTaskId(taskId)

    }

    override fun initListener() {

        mViewModel.loadingLiveData().observe(this, Observer {
            if(it){
                showLoading()
            }else{
                closeLoading()
            }
        })


        back.setOnClickListener {
            finish()
        }

        binding.itemDepartment.setOnClickListener {
            val intent = Intent(this,ConfigInfoActivity::class.java)
            val bundle = Bundle()
            bundle.putInt("PAGE_TYPE",PageType.TYPE_DEPARTMENT)
            intent.putExtras(bundle)
            startActivityForResult(intent,PageType.TYPE_DEPARTMENT)
        }

        binding.itemUser.setOnClickListener {
            if(departmentInfo != null){
                val intent = Intent(this,ConfigInfoActivity::class.java)
                val bundle = Bundle()
                bundle.putInt("PAGE_TYPE",PageType.TYPE_USER)
                bundle.putString("CODE",departmentInfo!!.code)
                intent.putExtras(bundle)
                startActivityForResult(intent,PageType.TYPE_USER)
            } else {
                toast("请先选择使用部门")
            }
        }

        binding.itemLocation.setOnClickListener {
            val intent = Intent(this,ConfigInfoActivity::class.java)
            val bundle = Bundle()
            bundle.putInt("PAGE_TYPE",PageType.TYPE_LOCATION)
            intent.putExtras(bundle)
            startActivityForResult(intent,PageType.TYPE_LOCATION)
        }

        binding.btnConfirm.setOnClickListener {
            if(departmentInfo != null && userInfo == null){
                toast("请选择使用人")
                return@setOnClickListener
            }

            if(userInfo == null && locationInfo == null){
                toast("请选择修改属性")
                return@setOnClickListener
            }
            mViewModel.confirmModify(departmentInfo,userInfo,locationInfo)
        }

        mViewModel.observerModifyStatus().observe(this, Observer {
            if(it){
                val intent = Intent()
                val bundle = Bundle()
                bundle.putSerializable("TASK_DETAIL",mViewModel.detailBean)
                intent.putExtras(bundle)
                setResult(RESULT_OK,intent)
                toast("修改成功")
                finish()
            }else{
                if(BaseApp.instance.isTest){
                    val intent = Intent()
                    val bundle = Bundle()
                    bundle.putSerializable("TASK_DETAIL",mViewModel.detailBean)
                    intent.putExtras(bundle)
                    setResult(RESULT_OK,intent)
                }
                toast("修改失败")
                finish()
            }
        })
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if(resultCode != RESULT_OK){
            return
        }
        when(requestCode){
            PageType.TYPE_DEPARTMENT -> {
                departmentInfo = data?.extras?.getSerializable("CONFIG_INFO") as ConfigInfo
                mViewModel.userDepartment.set(departmentInfo?.name)
                if(userInfo != null){
                    departmentInfo?.code?.let { it ->
                        val userList = ConfigInfoManager.getUserList(it).filter { userInfo!!.code != it.username }
                        if(userList.isNotEmpty()){
                            mViewModel.userName.set("")
                            userInfo = null
                        }

                    }
                }

            }

            PageType.TYPE_USER -> {
                userInfo = data?.extras?.getSerializable("CONFIG_INFO") as ConfigInfo
                mViewModel.userName.set(userInfo?.name)
            }

            PageType.TYPE_LOCATION -> {
                locationInfo = data?.extras?.getSerializable("CONFIG_INFO") as ConfigInfo
                mViewModel.location.set(locationInfo?.name)
            }
        }

        super.onActivityResult(requestCode, resultCode, data)
    }


    fun closeLoading(){
        binding.loading.visibility = View.GONE
        binding.avi.smoothToHide()
    }


    fun showLoading(){
        binding.loading.visibility = View.VISIBLE
        binding.avi.smoothToShow()
    }

}